package com.xunua.mymycsjsdktest;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.SnackbarUtils;
import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.FilterWord;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTBannerAd;
import com.bytedance.sdk.openadsdk.TTDrawFeedAd;
import com.bytedance.sdk.openadsdk.TTFeedAd;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.bytedance.sdk.openadsdk.TTImage;
import com.bytedance.sdk.openadsdk.TTInteractionAd;
import com.bytedance.sdk.openadsdk.TTNativeAd;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.xunua.mymycsjsdktest.dialog.DislikeDialog;

import java.util.List;



public class MainActivity extends AppCompatActivity {

    private TTAdNative adNative;
    private TextView FeedAd_tv;
    private ImageView FeedAd_img;
    private FrameLayout nativeView;
    private FrameLayout framelayout_TTNativeExpressAd;

    @RequiresApi(api = Build.VERSION_CODES.M)//动态权限获取是api23之后才有的
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRequestPermissions();
    }

    /**
     * 获取所需权限
     */
    private void initRequestPermissions() {
        List<String> permissions = PermissionUtils.getPermissions(Utils.getApp().getPackageName());//获取清单文件中声明的权限
        String[] permission = (String[]) permissions.toArray();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permission, 00000);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 00000) {//不考虑未给权限
            initTTAdManager();
        }
    }

    private void initTTAdManager() {
        //一定要在初始化后才能调用，否则为空
        TTAdManager ttAdManager = TTAdSdk.getAdManager();//TTAdManager对象为整个SDK的入口接口，可用于广告获取、权限请求、版本号获取等。
        ttAdManager.requestPermissionIfNecessary(this);//部分机型需要主动申请权限，如 READ_PHONE_STATE权限
        adNative = ttAdManager.createAdNative(this);//TTAdNative为加载广告的接口，用于加载信息流广告、Banner广告、开屏广告、插屏广告等，并提供了加载回调监听器，建议是Activity的成员变量。
    }

    private void initAdSlot() {
//        AdSlot adSlot = new AdSlot.Builder()
//                // 必选参数 设置您的CodeId
//                .setCodeId("900486272")
//                        // 必选参数 设置广告图片的最大尺寸及期望的图片宽高比，单位Px
//                        // 注：必填字段，期望的图片尺寸，返回尺寸可能有差异
//                        .setImageAcceptedSize(640, 320)
//                        //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看
////                        .setExpressViewAcceptedSize(500, 500)
//                        // 可选参数 设置是否支持deeplink
//                        .setSupportDeepLink(true)
//                        // 可选参数，针对信息流广告设置每次请求的广告返回个数，最多支持3个
//                        .setAdCount(2)
//                        //请求原生广告时候需要设置，参数为TYPE_BANNER或TYPE_INTERACTION_AD
//                        .setNativeAdType(AdSlot.TYPE_BANNER)
//                        //激励视频奖励的名称，针对激励视频参数
//                        .setRewardName("金币")
//                        //激励视频奖励个数
//                        .setRewardAmount(3)
//                        //用户ID,使用激励视频必传参数
//                        //表来标识应用侧唯一用户；若非服务器回调模式或不需sdk透传，可设置为空字符串
//                        .setUserID("user123")
//                        //设置期望视频播放的方向，为TTAdConstant.HORIZONTAL或TTAdConstant.VERTICAL
//                        .setOrientation(TTAdConstant.VERTICAL)
//                        //激励视频奖励透传参数，字符串，如果用json对象，必须使用序列化为String类型,可为空
//                        .setMediaExtra("media_extra")
//                        .build();


        //step4:创建feed广告请求类型参数AdSlot,具体参数含义参考文档
        final AdSlot adSlot = new AdSlot.Builder()
                .setCodeId("901121737")
                .setSupportDeepLink(true)
                .setImageAcceptedSize(640, 320)
                .setAdCount(1) //请求广告数量为1到3条
                .build();

        //加载原生信息流广告
        adNative.loadFeedAd(adSlot, new TTAdNative.FeedAdListener() {
            @Override
            public void onError(int i, String s) {
                //加载失败的回调，详见3.1错误码说明
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onFeedAdLoad(List<TTFeedAd> list) {
                //加载成功的回调 请确保您的代码足够健壮，可以处理异常情况；
                if (list == null || list.isEmpty()) {
                    Toast.makeText(MainActivity.this, "on FeedAdLoaded: ad is null!", Toast.LENGTH_SHORT).show();
                    return;
                }
                final TTFeedAd ttFeedAd = list.get(0);
                ttFeedAd.setActivityForDownloadApp(MainActivity.this);
                final String title = ttFeedAd.getTitle();
                Log.e("TAG", "onFeedAdLoad: " + title);
                FeedAd_tv = (TextView) findViewById(R.id.FeedAd_tv);
                FeedAd_tv.setVisibility(View.VISIBLE);
                FeedAd_tv.setText(title);
                FeedAd_img = findViewById(R.id.FeedAd_img);
                if (ttFeedAd.getAdView() != null) {
                    FeedAd_img.setVisibility(View.GONE);
                    FeedAd_tv.setVisibility(View.GONE);
                    Intent intent = new Intent(MainActivity.this, FeedAdVideoActivity.class);
                    MyApplication application = (MyApplication) getApplication();
                    application.setFeedAdVideoView(ttFeedAd.getAdView());
                    startActivity(intent);
                    return;
                }
                FeedAd_tv.setVisibility(View.VISIBLE);
                FeedAd_img.setVisibility(View.VISIBLE);
                TTImage image = (TTImage) ttFeedAd.getImageList().get(0);
                Glide.with(MainActivity.this)
                        .load(image.getImageUrl())
//                        .override(600, 200)
                        .fitCenter()
                        .into(FeedAd_img);
                FeedAd_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        SnackbarUtils.with(view).setMessage("点击了广告："+title).show();
                    }
                });
            }
        });
    }

    public void click01(View view) {
        initAdSlot();
    }

    public void click02(View view) {
        initAdBanner();
    }

    private void initAdBanner() {
        /**
         * 展示 901121895
         * 下载 901121987
         */
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId("901121987") //广告位id
                .setSupportDeepLink(true)
//                .setImageAcceptedSize(600, 500)//默认math_parent
                .setAdCount(3)
                .build();
        adNative.loadBannerAd(adSlot, new TTAdNative.BannerAdListener() {
            @Override
            public void onError(int i, String s) {
//                加载失败的回调 详见3.1错误码说明
                Toast.makeText(MainActivity.this, "load error : " + i + ", " + s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onBannerAdLoad(TTBannerAd ttBannerAd) {
//                加载成功的回调，接入方可在此处做广告的展示，请确保您的代码足够健壮，能够处理异常情况；
                View bannerView = ttBannerAd.getBannerView();
                if (bannerView == null) {
                    return;
                }
                LinearLayout linearLayout = (LinearLayout) findViewById(R.id.line_banner);
                linearLayout.removeAllViews();
                linearLayout.addView(bannerView);
                ttBannerAd.setSlideIntervalTime(30000);//30秒轮播
                int interactionType = ttBannerAd.getInteractionType();
                SnackbarUtils.with(bannerView).setMessage(interactionType+"").show();
                //（可选）设置下载类广告的下载监听
                bindDownloadListener(ttBannerAd);
            }
        });
    }

    private boolean mHasShowDownloadActive = false;

    private void bindDownloadListener(TTBannerAd ad) {
        ad.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {
                Toast.makeText(MainActivity.this, "点击图片开始下载", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                if (!mHasShowDownloadActive) {
                    mHasShowDownloadActive = true;
                    Toast.makeText(MainActivity.this, "下载中，点击图片暂停", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                Toast.makeText(MainActivity.this, "下载暂停，点击图片继续", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                Toast.makeText(MainActivity.this, "下载失败，点击图片重新下载", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onInstalled(String fileName, String appName) {
                Toast.makeText(MainActivity.this, "安装完成，点击图片打开", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                Toast.makeText(MainActivity.this, "点击图片安装", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     *             loadInteractionAd("901121725");      //下载
     *             loadInteractionAd("901121417");      //展示
     * @param view
     */
    public void TTInteractionAd(View view) {
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId("901121417")
                .setSupportDeepLink(true)
                .setImageAcceptedSize(600, 600) //根据广告平台选择的尺寸，传入同比例尺寸
                .build();
        adNative.loadInteractionAd(adSlot, new TTAdNative.InteractionAdListener() {
            @Override
            public void onError(int i, String s) {
                //网络加载失败

            }

            @Override
            public void onInteractionAdLoad(TTInteractionAd ttInteractionAd) {
                //网络加载成功
//                Toast.makeText(MainActivity.this, "加载成功:"+ttInteractionAd.getInteractionType(), Toast.LENGTH_SHORT).show();
                ttInteractionAd.setAdInteractionListener(new TTInteractionAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked() {
                        //被点击
                    }

                    @Override
                    public void onAdShow() {
                        //被展示
                    }

                    @Override
                    public void onAdDismiss() {
                        //插屏广告消失
                        Toast.makeText(MainActivity.this, "广告消失", Toast.LENGTH_SHORT).show();
                    }
                });
                ttInteractionAd.setShowDislikeIcon(new TTAdDislike.DislikeInteractionCallback() {
                    @Override
                    public void onSelected(int position, String value) {
                        //TToast.show(mContext, "反馈了 " + value);
//                        TToast.show(mContext,"\t\t\t\t\t\t\t感谢您的反馈!\t\t\t\t\t\t\n我们将为您带来更优质的广告体验",3);
                    }

                    @Override
                    public void onCancel() {
//                        TToast.show(mContext, "点击取消 ");
                    }

                    @Override
                    public void onRefuse() {
//                        TToast.show(mContext,"您已成功提交反馈，请勿重复提交哦！",3);
                    }
                });
                if (ttInteractionAd.getInteractionType()==TTAdConstant.INTERACTION_TYPE_DOWNLOAD){
                    //如果是下载类型的广告，可以注册下载状态回调监听
                    ttInteractionAd.setDownloadListener(new TTAppDownloadListener() {
                        @Override
                        public void onIdle() {
                            //点击了开始下载
                        }

                        @Override
                        public void onDownloadActive(long l, long l1, String s, String s1) {
                            //下载中
                        }

                        @Override
                        public void onDownloadPaused(long l, long l1, String s, String s1) {
                            //下载暂停
                        }

                        @Override
                        public void onDownloadFailed(long l, long l1, String s, String s1) {
                            //下载失败
                        }

                        @Override
                        public void onDownloadFinished(long l, String s, String s1) {
                            //下载完成
                        }

                        @Override
                        public void onInstalled(String s, String s1) {
                            //安装完成
                        }
                    });
                }
                ttInteractionAd.showInteractionAd(MainActivity.this);
            }
        });
    }

    public void TTNativeAd(View view) {
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId("901121423")
                .setSupportDeepLink(true)
                .setImageAcceptedSize(600, 257)
                .setNativeAdType(AdSlot.TYPE_BANNER)
                .setAdCount(1)
                .build();
        adNative.loadNativeAd(adSlot, new TTAdNative.NativeAdListener() {
            @Override
            public void onError(int i, String s) {
                //加载错误
            }

            @Override
            public void onNativeAdLoad(List<TTNativeAd> list) {
                TTNativeAd nativeAd = list.get(0);
                if (nativeAd ==null)
                    return;
                nativeView =findViewById(R.id.TTNativeAd_framelayout);
                View bannerView = getLayoutInflater().inflate(R.layout.native_ad, nativeView, false);
                if (bannerView == null) {
                    return;
                }
                nativeView.removeAllViews();
                nativeView.addView(bannerView);
                nativeAd.setActivityForDownloadApp(MainActivity.this);
                /**
                 * 将数据展示
                 */
                ((TextView) nativeView.findViewById(R.id.tv_native_ad_title)).setText(nativeAd.getTitle());
                ((TextView) nativeView.findViewById(R.id.tv_native_ad_desc)).setText(nativeAd.getDescription());
                ImageView imgDislike = nativeView.findViewById(R.id.img_native_dislike);
                bindDislikeAction(nativeAd, imgDislike);//接入网盟的dislike 逻辑，有助于提示广告精准投放度
                //2:在浏览器打开网页，3:在app中打开，4:下载应用，5:拨打电话 其它：未知类型
                Toast.makeText(MainActivity.this, ""+nativeAd.getInteractionType(), Toast.LENGTH_SHORT).show();
                if (nativeAd.getImageList() != null && !nativeAd.getImageList().isEmpty()) {
                    TTImage image = nativeAd.getImageList().get(0);
                    if (image != null && image.isValid()) {
                        ImageView im = nativeView.findViewById(R.id.iv_native_image);
                        Glide.with(MainActivity.this).load(image.getImageUrl()).into(im);
                    }
                }
                TTImage icon = nativeAd.getIcon();
                if (icon != null && icon.isValid()) {
                    ImageView im = nativeView.findViewById(R.id.iv_native_icon);
                    Glide.with(MainActivity.this).load(icon.getImageUrl()).into(im);
                }
                Button mCreativeButton = (Button) nativeView.findViewById(R.id.btn_native_creative);

                nativeAd.registerViewForInteraction(nativeView, mCreativeButton, new TTNativeAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, TTNativeAd ttNativeAd) {
                        //广告点击的回调，点击后的动作由sdk控制
                    }

                    @Override
                    public void onAdCreativeClick(View view, TTNativeAd ttNativeAd) {
                        //创意广告点击回调
                    }

                    @Override
                    public void onAdShow(TTNativeAd ttNativeAd) {
                        //广告展示回调，每个广告仅回调一次
                    }
                });

                //可根据广告类型，为交互区域设置不同提示信息
                switch (nativeAd.getInteractionType()) {
                    case TTAdConstant.INTERACTION_TYPE_DOWNLOAD:
                        //如果初始化ttAdManager.createAdNative(getApplicationContext())没有传入activity 则需要在此传activity，否则影响使用Dislike逻辑
                        nativeAd.setActivityForDownloadApp(MainActivity.this);
                        mCreativeButton.setVisibility(View.VISIBLE);
                        nativeAd.setDownloadListener(new TTAppDownloadListener() {
                            @Override
                            public void onIdle() {
                                //开始下载
                            }

                            @Override
                            public void onDownloadActive(long l, long l1, String s, String s1) {
                                //下载中
                            }

                            @Override
                            public void onDownloadPaused(long l, long l1, String s, String s1) {
                                //下载暂停
                            }

                            @Override
                            public void onDownloadFailed(long l, long l1, String s, String s1) {
                                //下载出错
                            }

                            @Override
                            public void onDownloadFinished(long l, String s, String s1) {
                                //下载完成
                            }

                            @Override
                            public void onInstalled(String s, String s1) {
                                //安装
                            }
                        });
                        break;
                    case TTAdConstant.INTERACTION_TYPE_DIAL:
                        mCreativeButton.setVisibility(View.VISIBLE);
                        mCreativeButton.setText("立即拨打");
                        break;
                    case TTAdConstant.INTERACTION_TYPE_LANDING_PAGE:
                    case TTAdConstant.INTERACTION_TYPE_BROWSER:
                        mCreativeButton.setVisibility(View.VISIBLE);
                        mCreativeButton.setText("查看详情");
                        break;
                    default:
                        mCreativeButton.setVisibility(View.GONE);
                        Toast.makeText(MainActivity.this, "交互类型异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void bindDislikeAction(TTNativeAd ad, View dislikeView) {
        final TTAdDislike ttAdDislike = ad.getDislikeDialog(this);
        if (ttAdDislike != null) {
            ttAdDislike.setDislikeInteractionCallback(new TTAdDislike.DislikeInteractionCallback() {
                @Override
                public void onSelected(int position, String value) {
                    //他人提交不感兴趣
                    nativeView.removeAllViews();
                }

                @Override
                public void onCancel() {
                    //广告消失
                }

                @Override
                public void onRefuse() {

                }
            });
        }
        dislikeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ttAdDislike != null)
                    ttAdDislike.showDislikeDialog();
            }
        });
    }

    /**
     * 是否竖屏
     * @param isVertical
     */
    public void TTRewardVideoAd(boolean isVertical) {
//        ("horizontal_rit","901121430");
//        ("vertical_rit","901121365");
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(isVertical?"901121365":"901121430")
                .setSupportDeepLink(true)
                .setAdCount(2)
                //个性化模板广告需要设置期望个性化模板广告的大小，单位dp，激励视频场景，只要设置的值大于0即可
//                .setExpressViewAcceptedSize(500, 500)
                .setImageAcceptedSize(1080, 1920)
                .setRewardName("金币")
                .setRewardAmount(3)//奖励的数量
                //必传参数，来标识应用例唯一用户；若非服务器回调模式 或不需sdk透传，则可设置为空字符串
                .setUserID("user123")
                .setOrientation(isVertical?TTAdConstant.VERTICAL:TTAdConstant.HORIZONTAL)//可设置横屏竖屏
                .setMediaExtra("media_extra")//用户透传的信息，可不传
                .build();
        adNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                //出错
                Toast.makeText(MainActivity.this, i+"------------------"+s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardVideoCached() {
                //视频广告加载后的视频文件资源缓存到本地的回调
            }

            @Override
            public void onRewardVideoAdLoad(final TTRewardVideoAd ttRewardVideoAd) {

                //视频广告素材的加载，如title，视频url等，不包含视频文件
                ttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
                    @Override
                    public void onAdShow() {
                        //广告被展示
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        //广告被被点击
                    }

                    @Override
                    public void onAdClose() {
                        //广告被关闭
                    }

                    @Override
                    public void onVideoComplete() {
                        //广告播放完毕
                    }

                    @Override
                    public void onVideoError() {
                        //视频出错
                    }

                    @Override
                    public void onRewardVerify(boolean b, int i, String s) {
                        //视频广告播完验证奖励有效性回调，参数分别为是否有效，奖励数量，奖励名称
                        Toast.makeText(MainActivity.this, b?"本次观看生效，获得奖励:"+s+"数量："+i:"本次观看无效", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSkippedVideo() {
                        //视频跳过
                    }
                });

                ttRewardVideoAd.setDownloadListener(new TTAppDownloadListener() {
                    @Override
                    public void onIdle() {
                        //点击了下载 开始下载
                    }

                    @Override
                    public void onDownloadActive(long l, long l1, String s, String s1) {
                        //正在下载
                    }

                    @Override
                    public void onDownloadPaused(long l, long l1, String s, String s1) {
                        //下载暂停
                    }

                    @Override
                    public void onDownloadFailed(long l, long l1, String s, String s1) {
                        //下载失败
                    }

                    @Override
                    public void onDownloadFinished(long l, String s, String s1) {
                        //下载完成
                    }

                    @Override
                    public void onInstalled(String s, String s1) {
                        //安装完成
                    }
                });
                ttRewardVideoAd.setShowDownLoadBar(true);//是否展示下载栏
                ttRewardVideoAd.showRewardVideoAd(MainActivity.this);
            }
        });
    }

    /**
     * 可跳过的全屏广告视频
     * @param isVertical  是否竖屏
     *
     */
    public void TTFullScreenVideoAd(boolean isVertical) {
        final String mHorizontalCodeId="901121184";//横屏的codeid
        final String mVerticalCodeId="901121375";//竖屏的codeid
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(isVertical?mVerticalCodeId:mHorizontalCodeId)
                .setSupportDeepLink(true)
                //个性化广告模板添加这个，用来设置全屏视频场景。。。。。只要设置的值大于0即可 单位dp
//                .setExpressViewAcceptedSize(500,500)
                .setImageAcceptedSize(1080, 1920)
                .setOrientation(isVertical?TTAdConstant.VERTICAL:TTAdConstant.HORIZONTAL)
                .build();
        adNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                //异常错误
                Toast.makeText(MainActivity.this, "TTFullScreenVideoAd出错,错误码："+i+"错误原因："+s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ttFullScreenVideoAd) {
                //加载完毕
                ttFullScreenVideoAd.setShowDownLoadBar(true);
                ttFullScreenVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {

                    @Override
                    public void onAdShow() {
                        //展示
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        //视频被点击
                    }

                    @Override
                    public void onAdClose() {
                        //广告关闭
                    }

                    @Override
                    public void onVideoComplete() {
                        //视频播放完成
                    }

                    @Override
                    public void onSkippedVideo() {
                        //视频跳过

                    }

                });
                ttFullScreenVideoAd.showFullScreenVideoAd(MainActivity.this);
            }

            @Override
            public void onFullScreenVideoCached() {
                //视频缓存回调
            }
        });
    }

    public void TTRewardVideoAd_shu(View view) {
        TTRewardVideoAd(true);
    }

    public void TTRewardVideoAd_heng(View view) {
        TTRewardVideoAd(false);
    }


    public void TTFullScreenVideoAd_heng(View view) {
        TTFullScreenVideoAd(false);
    }

    public void TTFullScreenVideoAd_shu(View view) {
        TTFullScreenVideoAd(true);
    }

    public void TTDrawFeedAd(View view) {
        startActivity(new Intent(this,TTDrawFeedActivity.class));
    }

    public void TTNativeExpressAd(View view) {
        startActivity(new Intent(this,NativeExpressActivity.class));
    }

//        mBannerAdSizeModelList.add(new AdSizeModel("600*90", 300, 45, "901121246"));
//        mBannerAdSizeModelList.add(new AdSizeModel("600*150", 300, 75, "901121700"));
//        mBannerAdSizeModelList.add(new AdSizeModel("600*260", 300, 130, "901121148"));
//        mBannerAdSizeModelList.add(new AdSizeModel("600*300", 300, 150, "901121228"));
//        mBannerAdSizeModelList.add(new AdSizeModel("600*400", 300, 200, "901121686"));
//        mBannerAdSizeModelList.add(new AdSizeModel("640*100", 320, 50, "901121223"));
//        mBannerAdSizeModelList.add(new AdSizeModel("690*388", 345, 194, "901121158"));
//        mBannerAdSizeModelList.add(new AdSizeModel("600*500", 300, 250, "901121834"));

    private TTNativeExpressAd ttNativeExpressAd;
    public void nativeExpressAdBanner(int width,int height,String codeId) {
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId)
                .setSupportDeepLink(true)
                .setAdCount(1)
                .setExpressViewAcceptedSize(width, height)
                .setImageAcceptedSize(640, 320)
                .build();
        adNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                if (list == null || list.size() == 0) {
                    return;
                }
                ttNativeExpressAd = list.get(0);
                ttNativeExpressAd.setSlideIntervalTime(30*1000);//设置轮播间隔ms 不调用则不进行轮播
                bindAdListener(ttNativeExpressAd);
                ttNativeExpressAd.render();//开始渲染广告
            }
        });
    }

    //绑定广告渲染
    private void bindAdListener(TTNativeExpressAd ad) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int i) {
                //广告被点击
            }

            @Override
            public void onAdShow(View view, int i) {
                //广告展示
            }

            @Override
            public void onRenderFail(View view, String s, int i) {
                //渲染失败
            }

            @Override
            public void onRenderSuccess(View view, float v, float v1) {
                //渲染成功
                framelayout_TTNativeExpressAd=findViewById(R.id.framelayout_TTNativeExpressAd);
                framelayout_TTNativeExpressAd.removeAllViews();
                framelayout_TTNativeExpressAd.addView(view);
            }
        });

        //dislike设置
        bindDislike(ad, false);
        if (ad.getInteractionType()!=TTAdConstant.INTERACTION_TYPE_DOWNLOAD){
            return;
        }
        Log.e("TAG", "bindAdListener: 下载类型");
        ad.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {
                //未开始下载
            }

            /**
             * 下载中回调
             * @param totalBytes 安装包总字节数 -1 ：未知
             * @param currBytes 当前已下载的字节数
             * @param fileName 下载文件名称
             * @param appName 当前下载的APP名
             */
            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                //下载中
            }

            /**
             * 下载暂停回调
             * @param totalBytes 安装包总字节数 -1 ：未知
             * @param currBytes 当前已下载的字节数
             * @param fileName 下载文件名称
             * @param appName 当前下载的APP名
             */
            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                //下载暂停
            }

            /**
             * 下载失败回调
             * @param totalBytes 安装包总字节数 -1 ：未知
             * @param currBytes 当前已下载的字节数
             * @param fileName 下载文件名称
             * @param appName 当前下载的APP名
             */
            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                //下载失败回调
            }

            /**
             * 下载完成回调
             * @param totalBytes 安装包总字节数 -1 ：未知
             * @param fileName 下载文件名称
             * @param appName 当前下载的APP名
             */
            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                //下载完成
            }

            /**
             * 安装完成回调
             * @param fileName 下载文件名称
             * @param appName 当前下载的APP名
             */
            @Override
            public void onInstalled(String fileName, String appName) {
                //安装完成回调
            }
        });
    }

    /**
     * 设置广告的不喜欢，开发者可自定义样式
     * @param ad
     * @param customStyle 是否自定义样式，true:样式自定义
     */
    private void bindDislike(TTNativeExpressAd ad, boolean customStyle) {
        if (customStyle) {
            //使用自定义样式
            List<FilterWord> words = ad.getFilterWords();
            if (words == null || words.isEmpty()) {
                return;
            }

            final DislikeDialog dislikeDialog = new DislikeDialog(this, words);
            dislikeDialog.setOnDislikeItemClick(new DislikeDialog.OnDislikeItemClick() {
                @Override
                public void onItemClick(FilterWord filterWord) {
                    //屏蔽广告
//                    TToast.show(mContext, "点击 " + filterWord.getName());
                    //用户选择不喜欢原因后，移除广告展示
                    framelayout_TTNativeExpressAd.removeAllViews();
                }
            });
            ad.setDislikeDialog(dislikeDialog);
            return;
        }
        //使用默认个性化模板中默认dislike弹出样式
        ad.setDislikeCallback(MainActivity.this, new TTAdDislike.DislikeInteractionCallback() {
            @Override
            public void onSelected(int position, String value) {
//                TToast.show(mContext, "点击 " + value);
                //用户选择不喜欢原因后，移除广告展示
                framelayout_TTNativeExpressAd.removeAllViews();
            }

            @Override
            public void onCancel() {
//                TToast.show(mContext, "点击取消 ");
            }

            @Override
            public void onRefuse() {
                //拒绝
            }
        });
    }


    public void TTNativeExpressAdBanner600_90(View view) {
        nativeExpressAdBanner(300,45,"901121246");
    }

    public void TTNativeExpressAdBanner600_150(View view) {
        nativeExpressAdBanner(300,75,"901121700");
    }

    public void TTNativeExpressAdBanner600_260(View view) {
        nativeExpressAdBanner(300,130,"901121148");
    }

    public void TTNativeExpressAdBanner600_300(View view) {
        nativeExpressAdBanner(300,150,"901121228");
    }

    public void TTNativeExpressAdBanner600_400(View view) {
        nativeExpressAdBanner(300,200,"901121686");
    }

    public void TTNativeExpressAdBanner640_100(View view) {
        nativeExpressAdBanner(320,50,"901121223");
    }

    public void TTNativeExpressAdBanner690_388(View view) {
        nativeExpressAdBanner(345,194,"901121158");
    }

    public void TTNativeExpressAdBanner600_500(View view) {
        nativeExpressAdBanner(300,250,"901121834");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ttNativeExpressAd != null) {
            //调用destroy()方法释放
            ttNativeExpressAd.destroy();
        }
        if (ttNativeExpressAdBanner != null) {
            ttNativeExpressAdBanner.destroy();
        }
    }

    private TTNativeExpressAd ttNativeExpressAdBanner;
    public void TTNativeExpressAdBanner(String CodeId) {
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(CodeId)
                .setSupportDeepLink(true)
                .setAdCount(1)
                .setExpressViewAcceptedSize(600, 0)
                .setImageAcceptedSize(640, 320)
                .build();
        adNative.loadInteractionExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int i, String s) {
                Toast.makeText(MainActivity.this, "出错"+s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                if (list==null||list.size()==0){
                    return;
                }
                ttNativeExpressAdBanner = list.get(0);
                ttNativeExpressAdBanner.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {
                    @Override
                    public void onAdDismiss() {
                        Toast.makeText(MainActivity.this, "广告关闭", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAdClicked(View view, int i) {
                        Toast.makeText(MainActivity.this, "广告被点击", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAdShow(View view, int i) {
                        Toast.makeText(MainActivity.this, "广告展示", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRenderFail(View view, String s, int i) {
                        Toast.makeText(MainActivity.this, "出错", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRenderSuccess(View view, float v, float v1) {
                        Toast.makeText(MainActivity.this, "渲染成功，开始展示广告", Toast.LENGTH_SHORT).show();
                    }
                });
                if (ttNativeExpressAdBanner.getInteractionType()==TTAdConstant.INTERACTION_TYPE_DOWNLOAD){

                }
                ttNativeExpressAdBanner.render();
                ttNativeExpressAdBanner.showInteractionExpressAd(MainActivity.this);
            }
        });
    }

    public void TTNativeExpressAdBanner1_1(View view) {
        TTNativeExpressAdBanner("901121797");
    }

    public void TTNativeExpressAdBanner2_3(View view) {
        TTNativeExpressAdBanner("901121133");
    }

    public void TTNativeExpressAdBanner3_2(View view) {
        TTNativeExpressAdBanner("943766446");
    }

    public void TTNativeExpressDrawFeedAd(View view) {
        //901121041
        startActivity(new Intent(this,TTNativeExpressDrawFeedAdActivity.class));
    }
}